using Microsoft.AspNetCore.Mvc;
using Model.Common;
using System;
using System.Threading.Tasks;

namespace srb.net.Controllers.Common
{
    /// <summary>
    /// API控制器基类
    /// 提供统一的返回结果方法
    /// </summary>
    [ApiController]
    public class BaseApiController : ControllerBase
    {
        /// <summary>
        /// 成功返回
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        protected IActionResult Success<T>(T data, string message = "操作成功")
        {
            return Ok(ApiResult<T>.SuccessResult(data, message));
        }

        /// <summary>
        /// 成功返回（无数据）
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns></returns>
        protected IActionResult Success(string message = "操作成功")
        {
            return Ok(ApiResult.SuccessResult(message));
        }

        /// <summary>
        /// 失败返回
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        protected IActionResult Error<T>(string message = "操作失败", int code = 500)
        {
            return StatusCode(code, ApiResult<T>.ErrorResult(message, code));
        }

        /// <summary>
        /// 失败返回（无数据）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        protected IActionResult Error(string message = "操作失败", int code = 500)
        {
            return StatusCode(code, ApiResult.ErrorResult(message, code));
        }

        /// <summary>
        /// 参数错误返回
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        protected IActionResult BadRequest<T>(string message = "参数错误")
        {
            return BadRequest(ApiResult<T>.BadRequestResult(message));
        }

        /// <summary>
        /// 参数错误返回（无数据）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        protected IActionResult BadRequest(string message = "参数错误")
        {
            return BadRequest(ApiResult.BadRequestResult(message));
        }

        /// <summary>
        /// 未授权返回
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        protected IActionResult Unauthorized<T>(string message = "未授权")
        {
            return Unauthorized(ApiResult<T>.UnauthorizedResult(message));
        }

        /// <summary>
        /// 未授权返回（无数据）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        protected IActionResult Unauthorized(string message = "未授权")
        {
            return Unauthorized(ApiResult.UnauthorizedResult(message));
        }

        /// <summary>
        /// 未找到返回
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        protected IActionResult NotFound<T>(string message = "未找到")
        {
            return NotFound(ApiResult<T>.NotFoundResult(message));
        }

        /// <summary>
        /// 未找到返回（无数据）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns></returns>
        protected IActionResult NotFound(string message = "未找到")
        {
            return NotFound(ApiResult.NotFoundResult(message));
        }

        /// <summary>
        /// 执行操作并返回结果
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="action">要执行的操作</param>
        /// <param name="successMessage">成功消息</param>
        /// <param name="errorMessage">错误消息</param>
        /// <returns></returns>
        protected async Task<IActionResult> ExecuteAsync<T>(Func<Task<T>> action, string successMessage = "操作成功", string errorMessage = "操作失败")
        {
            try
            {
                var result = await action();
                return Success(result, successMessage);
            }
            catch (Exception ex)
            {
                return Error<T>($"{errorMessage}：{ex.Message}");
            }
        }

        /// <summary>
        /// 执行操作并返回结果（无数据）
        /// </summary>
        /// <param name="action">要执行的操作</param>
        /// <param name="successMessage">成功消息</param>
        /// <param name="errorMessage">错误消息</param>
        /// <returns></returns>
        protected async Task<IActionResult> ExecuteAsync(Func<Task> action, string successMessage = "操作成功", string errorMessage = "操作失败")
        {
            try
            {
                await action();
                return Success(successMessage);
            }
            catch (Exception ex)
            {
                return Error($"{errorMessage}：{ex.Message}");
            }
        }

        /// <summary>
        /// 执行操作并返回结果
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="action">要执行的操作</param>
        /// <param name="successMessage">成功消息</param>
        /// <param name="errorMessage">错误消息</param>
        /// <returns></returns>
        protected IActionResult Execute<T>(Func<T> action, string successMessage = "操作成功", string errorMessage = "操作失败")
        {
            try
            {
                var result = action();
                return Success(result, successMessage);
            }
            catch (Exception ex)
            {
                return Error<T>($"{errorMessage}：{ex.Message}");
            }
        }

        /// <summary>
        /// 执行操作并返回结果（无数据）
        /// </summary>
        /// <param name="action">要执行的操作</param>
        /// <param name="successMessage">成功消息</param>
        /// <param name="errorMessage">错误消息</param>
        /// <returns></returns>
        protected IActionResult Execute(Action action, string successMessage = "操作成功", string errorMessage = "操作失败")
        {
            try
            {
                action();
                return Success(successMessage);
            }
            catch (Exception ex)
            {
                return Error($"{errorMessage}：{ex.Message}");
            }
        }
    }
}
